package com.java.rabbitmq.public_subject;

import com.java.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;

/**
 * @author Woo_home
 * @create 2020/5/26 14:45
 */

public class Producer_Public {

    private final static String EXCHANGE_NAME = "test_exchange_fanout";

    public static void main(String[] args) {
        try {
            //获取连接
            Connection connection = ConnectionUtils.getConnection();
            //从连接中获取一个通道
            Channel channel = connection.createChannel();
            //声明交换机（分发:发布/订阅模式）
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            //发送消息
            for (int i = 0; i < 10; i++) {
                String message = "this is user message" + i;
                System.out.println("[send]：" + message);
                //发送消息
                channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("utf-8"));
                Thread.sleep(5 * i);
            }
            channel.close();
            connection.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
